package com.aaa.servlet;

import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;

import com.aaa.annotation.RequestMapping;
import com.aaa.dao.CarDAO;
import com.aaa.dao.impl.CarDAOImpl;
import com.aaa.result.ResultEntity;

@WebServlet("/car/*")
public class CarServlet extends BaseServlet {
	
	
	private CarDAO  carDAO = new  CarDAOImpl();
	
	@RequestMapping(url="getCarInfo")
	public ResultEntity getCarInfo(Map<String,Object> args) {
		
		// 获取session中的用户
		Map<String,Object> user = (Map<String, Object>) session.getAttribute("user");
		Integer uid = (Integer) user.get("uid");
		
		// 根据uid获取对应的购物车信息
		List<Map<String, Object>> list = carDAO.listProductByUid(uid);
		
		return new ResultEntity(10032, "获取成功", list);
	}
	
	
	
	
	@RequestMapping(url="addCar")
	public ResultEntity addCar(Map<String,Object> args) {
		//1 判断用户是否登录  如果没有登录 则返回信息 没有登录 请先登录。
		Map<String,Object> user = (Map<String, Object>) session.getAttribute("user");
		if(user == null) return new ResultEntity(10030, "对不起 请先登录", null);
		//2 如果登录了  则判断当前商品 是否已经添加到购物车中          uid 哪个用户   pid 哪个商品
		String pid = (String) args.get("pid");
		Integer uid = (Integer) user.get("uid");
		boolean exist = carDAO.isAddCar(uid, pid);
		if( exist ) {
			//4 如果添加过购物车  则 进行数量的累加
			carDAO.updateProductNo(uid, args);
		}else {
			//3 如果没有添加过购物车  则将商品添加到购物车中
			carDAO.saveProductToCar(uid, args);
		}
		//5 返回添加成功的信息
		return  new ResultEntity(10031, "添加成功", null);
	}

}
